import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
import viteCompression from 'vite-plugin-compression'

// https://vite.dev/config/
export default defineConfig(({ mode }) => ({
  plugins: [
    react(),
    viteCompression({
      algorithm: 'gzip',
      ext: '.gz',
      threshold: 10240,
    })
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  },
  server: {
    port: 3000,
    host: '0.0.0.0',
    proxy: {
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
        rewrite: (path) => path
      },
      '/engine': {
        target: 'http://localhost:8080',
        changeOrigin: true,
        rewrite: (path) => path
      },
    }
  },
  build: {
    target: 'esnext',
    minify: 'terser',
    sourcemap: false,
    outDir: 'dist',
    assetsDir: 'assets',
    rollupOptions: {
      preserveEntrySignatures: 'strict',
      output: {
        manualChunks: {
          'vendor': [
            'react',
            'react-dom',
            'react-router-dom',
            'react-redux',
            'mobx',
            'mobx-react-lite'
          ],
          'antd': ['antd'],
          'wangeditor': ['@wangeditor/editor', '@wangeditor/editor-for-react'],
          'echarts': ['echarts'],
          'semi': ['@douyinfe/semi-ui'],
          'reactflow': ['reactflow'],
        }
      }
    }
  },
  esbuild: mode === 'production' ? { drop: ['console', 'debugger'] } : {},
  base: './'
}))